<?php

namespace App\Http\Controllers\VillagerCommittee;

use App\Enum\CodeEnum;
use App\Http\Controllers\Api\ApiBaseController;
use App\Services\CommonService;
use App\Services\VillageCommitteeService;
use App\Services\VillagerUserService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class VillagerCommitteeController extends ApiBaseController
{

    /**
     * 村委会审核.
     * @author ninetyseven.
     * @date 2023-04-17 23:39
     */
    public function stateAdd(Request $request)
    {
        try {
            $post = $request->post();

            $data = VillageCommitteeService::stateSave($post);
            return success();
        } catch (Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 预警列表
     * @return JsonResponse|null
     */
    public function waringOrderList(): ?JsonResponse
    {
        try {
            $data = VillageCommitteeService::waringOrderList();
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 预警订单详情
     * @param Request $request
     * @return JsonResponse|null
     */
    public function waringOrderDetail(Request $request): ?JsonResponse
    {
        try {
            $orderId = $request->get('order_id');
            if(!intval($orderId)){
                throw new \Exception('订单id错误');
            }

            $data = VillageCommitteeService::waringOrderDetail($orderId);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者分配列表
     * @param Request $request
     * @return JsonResponse|null
     */
    public function volunteersList(Request $request): ?JsonResponse
    {
        try {
            $search = $request->get('search','');
            $data = VillageCommitteeService::volunteersList($search);

            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者分配
     * @param Request $request
     * @return JsonResponse|null
     */
    public function volunteersAdd(Request $request): ?JsonResponse
    {
        try {
            $orderId = $request->get('order_id',0);
            $userId = $request->get('user_id',0);

            $res = VillageCommitteeService::volunteersAdd($orderId,$userId);
            if($res !== true){
                return fail(CodeEnum::ERROR, '分配失败');
            }

            return success();
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者统计
     * volunteersStatistics
     * @author ninetyseven.
     * @date 2023/5/26
     */
    public function volunteersStatistics()
    {
        try {
            $res = VillageCommitteeService::volunteersStatistics();
            return success($res);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 查询村民列表
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function userList(Request $request): JsonResponse
    {
        try {
            $params['status'] = $request->get('status');
            $params['search'] = $request->get('search');

            $params['community_id'] = CommonService::getCommunity()['community_id'];

            $data = VillagerUserService::getVillagerList($params);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 添加子账户
     * @param Request $request
     * @return JsonResponse|null
     */
    public function accountAdd(Request $request): ?JsonResponse
    {
        try {
            $post = $request->post();
            DB::beginTransaction();
            $res = VillageCommitteeService::accountAdd($post);
            if($res === false){
                DB::rollBack();
                return fail(CodeEnum::ERROR, '添加失败');
            }
            return success();
        } catch (Exception $e) {
            DB::rollBack();
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 子账户列表
     * @param Request $request
     * @return JsonResponse|null
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function accountList(Request $request): ?JsonResponse
    {
        try {
            $search = $request->get('search') ?? '';

            $data = VillageCommitteeService::getAccountList($search);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 子账户详情
     * @param Request $request
     * @return JsonResponse|null
     */
    public function accountDetail(Request $request): ?JsonResponse
    {
        try {
            $id = $request->get('id') ?? 0;
            $data = VillageCommitteeService::getAccountDetail($id);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    public function accountDelete(Request $request): ?JsonResponse
    {
        try {
            $id = $request->get('id') ?? 0;
            $res = VillageCommitteeService::accountDelete($id);
            if($res === false){
                return fail(CodeEnum::ERROR, '删除失败');
            }
            return success();
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
}
